package com.lisx.module_matters.dialogfragment;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.MattersDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.manager.MattersManager;
import com.ijianji.lsx_ui_lib.BaseDialogFragment;
import com.lisx.module_matters.R;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddMattersNewDialogFragment extends BaseDialogFragment {

    int type = 1;
    MattersDbEntity mattersDbEntity;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_fragment_add_matters_new;
    }

    @Override
    public void initView(Context context, View view, AttributeSet attributeSet) {
        EditText etMatters = view.findViewById(R.id.et_matters);
        if (mattersDbEntity != null) {
            type = mattersDbEntity.getType();
            etMatters.setText(mattersDbEntity.getMatters_name());
        }
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_complete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String matters = etMatters.getText().toString().trim();
                if (TextUtils.isEmpty(matters)) {
                    ToastUtils.showShort("请输入事项名称");
                }
                saveMatters(matters);
                dismiss();
            }
        });

    }

    public void setType(int type) {
        this.type = type;
    }

    private void saveMatters(String mattersName) {
        MattersDbEntity entity = new MattersDbEntity();
        entity.setMatters_name(mattersName);
        entity.setType(type);
        entity.setCreate_time(System.currentTimeMillis());
        Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {
                if (mattersDbEntity == null) {
                    MattersManager.getInstance().saveMattersDbEntity(entity);
                } else {
                    mattersDbEntity.setMatters_name(mattersName);
                    MattersManager.getInstance().saveMattersDbEntity(mattersDbEntity);
                }
                emitter.onNext(true);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Boolean aBoolean) {
                        EventBus.getDefault().post(new EventEntity(EventBusConstants.UPDATE_MATTERS));
                        if (mattersDbEntity == null) {
                            ToastUtils.showShort("添加成功");
                        } else {
                            ToastUtils.showShort("修改成功");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (mattersDbEntity == null) {
                            ToastUtils.showShort("添加失败");
                        } else {
                            ToastUtils.showShort("修改失败");
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    public void setData(MattersDbEntity mattersDbEntity) {
        this.mattersDbEntity = mattersDbEntity;
    }
}
